package com.orchard.xpz.config;


import com.orchard.xpz.entity.sysUser.SysUser;
import com.orchard.xpz.utils.UserRuntime;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/21 10:13
 **/
public class AuthFilter implements Filter {


    public AuthFilter() {
        super();
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
//        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        if (request.getServletPath().equals("/login") || request.getServletPath().equals("/logOut")){
            filterChain.doFilter(request,response);
            return;
        }
        Object serverLoginUser = request.getSession().getAttribute("token");
        SysUser user = (SysUser) serverLoginUser;
        System.out.println( "》》"+user);
        UserRuntime.setUser(user);
        filterChain.doFilter(request,response);
        return;

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
